/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author windsea
 */
public class ClientControl {
        ClientView view;
        User user;
        Registry registry;
        RMIinterface rmiinterface = null;
        
    public ClientControl(ClientView view){
        try {
        this.view = view;
        view.addLoginListener(new LoginListener());
        registry = LocateRegistry.getRegistry("localhost",9999);
        rmiinterface = (RMIinterface)(registry.lookup("rmi"));
        
        } catch (RemoteException ex) {
            Logger.getLogger(ClientControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException e){
            
        }
        
    }

    class LoginListener implements ActionListener {

        public LoginListener() {
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            try {           
                user = view.getUser();
                if(rmiinterface.checkLogin(user).equals("ok")){
                    view.showMessage("ok");
                }else{
                    view.showMessage("sorry");
                }
            } catch (RemoteException ex) {
                Logger.getLogger(ClientControl.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
                
        }
    }
}
